<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="form" label-width="100px" v-if="open_type != 'psw'">
      <el-form-item label="用户名" required prop="username">
        <el-input
          class="max-w460"
          v-model="form.username"
          placeholder="请输入负责人用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" required prop="name">
        <el-input class="max-w460" v-model="form.name" placeholder="请输入负责人姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" required prop="phone">
        <el-input class="max-w460" v-model="form.phone" placeholder="请输入负责人手机号"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { addManagerApi, updateManagerApi } from "@/api/babycar";
import { ElMessage } from "element-plus";
import { inject, ref, watch } from "vue";
const title = ref("编辑负责人");
const dialogVisible = ref(false);
const form = ref({
  manager_id: 0,
  username: "",
  name: "",
  phone: "",
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, row?: any) => {
  open_type.value = type;
  console.log(row);
  if (type == "edit") {
    title.value = "编辑负责人";
    form.value = {
      manager_id: row.manager_id,
      username: row.username,
      name: row.name,
      phone: row.phone,
    };
  } else if (type == "add") {
    title.value = "添加负责人";
    form.value = {
      manager_id: 0,
      username: "",
      name: "",
      phone: "",
    };
  }
  dialogVisible.value = true;
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        addManagerApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        updateManagerApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
/*取消*/
const cancelFunc = () => {
  loading.value = false;
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style lang="scss" scoped>
.map-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
